import json


path = "stockList.json"


def loadJson(path):
    with open(path, 'r') as f:
        stockList = json.load(f)
        # tmpDict = {}
        # for i in range(len(stockList)):
        #     tmpDict[stockList["alpha"] + str(i) + '1'] = stockList["name"]
        return stockList
        # count = 1
        # for stock in stockList():
        #     print(count)
        #     count += 1

def search(target, stockList):
    targetLen = len(target)
    all = [target]
    if targetLen > 5 or targetLen < 2:
        print("cao")
    elif targetLen == 3:
        for i in range(0, targetLen-1):
            all.append(target[i: i+2])
    elif targetLen == 4:
        for i in range(0, targetLen - 2):
            all.append(target[i: i + 3])

        for i in range(0, targetLen - 1):
            all.append(target[i: i + 2])
    printStock = {}
    already = []
    for stock in stockList:
        for strPeace in all:
            if strPeace not in printStock:
                printStock[strPeace] = []
            if strPeace in stock["alpha"].lower():
                if stock not in already:
                    printStock[strPeace].append(stock)
                    already.append(stock)

    return printStock


if __name__ == '__main__':
    stockList = loadJson(path)
    target = "qygk"
    printStock = search(target, stockList)
    for k, v in printStock.items():
        if len(v) == 0:
            continue
        print(k + ":")
        for i in v:
            print(i)